<%-- 
    Document   : recommendationMgmt
    Created on : Dec 14, 2011, 10:13:00 PM
    Author     : Randy
--%>

<%@page import="java.util.HashMap"%>
<%@page import="model.Recommendation"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Recommendation Management</title>
        <%@include file = "global/admin_include.jsp" %>
        <%
            HashMap am = new HashMap();
            Object obj = request.getSession().getAttribute("accessMap");
            if (obj == null) {
                response.sendRedirect("../admin.jsp");
            } else {

                am = (HashMap) obj;
                if (am.get("R").equals("false")) {
                    response.sendRedirect("accessDenied.jsp");
                }
            }  
        %>
        <script type="text/javascript">
            //only needed for numberspinner in grid
            $.extend($.fn.datagrid.defaults.editors, {
                numberspinner: {
                    init: function(container, options){
                        var input = $('<input type="text">').appendTo(container);
                        return input.numberspinner(options);
                    },
                    destroy: function(target){
                        $(target).numberspinner('destroy');
                    },
                    getValue: function(target){
                        return $(target).numberspinner('getValue');
                    },
                    setValue: function(target, value){
                        $(target).numberspinner('setValue',value);
                    },
                    resize: function(target, width){
                        $(target).numberspinner('resize',width);
                    }
                }
            });
            
            $(document).ready(function(){
                getThemes();
                
                $('#dd').dialog({modal:true});
                $('#dd').dialog('close');
                
                $('#dd_update').dialog({modal:true});
                $('#dd_update').dialog('close');
                
                $('#min').numberspinner({
                    precision:1,
                    min:1,
                    max:5,
                    increment:0.5,
                    editable:false
                });
                
                $('#max').numberspinner({
                    precision:1,
                    min:1,
                    max:5,
                    increment:0.5,
                    editable:false
                });
                
                $('#min_update').numberspinner({
                    precision:1,
                    min:1,
                    max:5,
                    increment:0.5,
                    editable:false
                });
                
                $('#max_update').numberspinner({
                    precision:1,
                    min:1,
                    max:5,
                    increment:0.5,
                    editable:false
                });
            });
            
            function getThemes(){
                $.ajax({
                    type: "POST",
                    url: "../ThemeServlet",
                    data: "action=getThemes&industry=<%=request.getParameter("industry")%>",
                    dataType: "xml",
                    cache: false,
                    success: function(data){
                        $('#theme_id').append('<option value=\'\'></option>');
                        $('#theme').append('<option value=\'\'></option>');
                        $(data).find('theme').each(function(){
                            $('#theme_id').append('<option value=\'' + $(this).find('theme_id').text() + '\'>' + $(this).find('theme_name').text() + '</option>');
                            $('#theme').append('<option value=\'' + $(this).find('theme_id').text() + '\'>' + $(this).find('theme_name').text() + '</option>');
                        });
                    }
                });
            }
            
            $(function() {
                $('#theme_id').change(function() {
                    $('#tt').datagrid({  
                        url:'../RecommendationServlet?action=getRecommendations&theme_id=' + $("#theme_id :selected").val()
                    });
                });
            });
            
            function getRecommendationInfo(recommend_id){
                $('#dd_update').dialog('open');
                $.ajax({
                    type: "POST",
                    url:"../RecommendationServlet",
                    data: "action=getRecommendationInfo"
                        +"&recommend_id="+recommend_id,
                    dataType: "json",
                    cache: false,
                    success: function(data){
                        if(data.type=='error'){ 
                            alertMsg(data);  
                            $('#dd_update').dialog('close');
                        }else{
                            $('#recommend_id_update').val(data.recommend_id);
                            $('#min_update').numberspinner('setValue',data.lower_range);
                            $('#max_update').numberspinner('setValue',data.upper_range);
                            $('#short_update').val(data.short_desc);
                            $('#long_update').val(data.long_desc);
                        }
                    }
                });
            }
            
            function formatActions(value,row,index){ 
                var e = '<input type="button" onclick="getRecommendationInfo('+row.recommend_id+')" value="Edit"/> ';
                var d = '<input type="button" onclick="deleteRecommendation('+row.recommend_id+')" value="Delete"/>';
                return e+d;  
            }
            
            function openDD(){
                $('#theme').val("");
                $('#min').numberspinner('setValue',1);
                $('#max').numberspinner('setValue',5);
                $('#short').val("");
                $('#long').val("");
                $('#dd').dialog('open');
            }
            
            function closeDD(){
                $('#dd').dialog('close');
            }
            
            function closeDDUpdate(){
                $('#dd_update').dialog('close');
            }
            
            function updateRecommendation(){
                if (parseFloat($('#min_update').val()) >= parseFloat($('#max_update').val())) {
                    alertMsgStr("Edit Recommendation", "Upper range must be more than lower range", "error");
                } else if ($('#short_update').val() == "" || $('#long_update').val() == "") {
                    alertMsgStr("Edit Recommendation", "All fields are required", "error");
                } else {
                    $.ajax({
                        type: "POST",
                        url: "../RecommendationServlet",
                        data: "action=updateRecommendation"
                            + "&theme=" + $("#theme_id :selected").val()
                            + "&recommend_id=" + $("#recommend_id_update").val()
                            + "&short=" + $("#short_update").val()
                            + "&long=" + $("#long_update").val()
                            + "&min=" + $("#min_update").val()
                            + "&max=" + $("#max_update").val(),
                        dataType: "json",
                        cache: false,
                        success: function(data){
                            if((data.type!='error')){
                                $('#tt').datagrid('reload');
                                closeDDUpdate();
                            }
                            alertMsg(data);
                        }
                    });
                }
            }
            
            function createRecommendation(){
                var theme = $("#theme :selected").val();
                var shortDesc = $("#short").val();
                var longDesc = $("#long").val();
                var min = $("#min").val();
                var max = $("#max").val();
                
                if (theme == "" || shortDesc == "" || longDesc == "" || min == "" || max == "") {
                    alertMsgStr("New Recommendation", "All fields are required", "error");
                    
                } else if (parseFloat(min) >= parseFloat(max)) {
                    alertMsgStr("New Recommendation", "Upper range must be more tham lower range", "error");
                    
                } else {
                    $.ajax({
                        type: "POST",
                        url: "../RecommendationServlet",
                        data: "action=createRecommendation"
                            + "&theme=" + theme
                            + "&short=" + shortDesc
                            + "&long=" + longDesc
                            + "&min=" + min
                            + "&max=" + max,
                        dataType: "json",
                        cache: false,
                        success: function(data){
                            if((data.type!='error')){
                                $("#theme_id").val(theme);
                                $('#dd').dialog('close');
                                $('#tt').datagrid('reload');
                            }
                            alertMsg(data);
                        }
                    });
                }
            }
            
            function deleteRecommendation(recommend_id){
                window.parent.$.messager.confirm("Confirm", "Delete recommendation?", function(r){
                    if (r){
                        $.ajax({
                            type: "POST",
                            url: "../RecommendationServlet",
                            data: "action=deleteRecommendation"
                                + "&theme_id=" + $("#theme_id :selected").val()
                                + "&recommend_id=" + recommend_id,
                            dataType: "json",
                            cache: false,
                            success: function(data){
                                if((data.type!='error')){
                                    $('#tt').datagrid('reload');
                                }
                                alertMsg(data);
                            }
                        });
                    }
                });
            }
        </script>
    </head>
    <body>
        <div style="border-bottom:1px solid #ccc"><h2>Recommendation Management</h2></div> 
        <br/>
        <div id="tb" style="padding:5px;height:auto">
            <table width="100%">
                <tr>
                    <td width="80%">Theme: <select name="theme_id" id="theme_id"></select></td>
                    <td width="20%" align="right"><input type="button" value="New Recommendation" onclick="openDD()"/></td>
                </tr>
            </table>
        </div>  
        <div style="height:450px">
            <table id="tt" class="easyui-datagrid" fit="true"
                   toolbar="#tb"
                   singleSelect="true" fitColumns="true" pagination="false"
                   rownumbers="false">
                <thead>  
                    <tr>
                        <th field="recommend_id" width="5%">ID</th>
                        <th field="short_desc" editor="{type:'text'}" width="15%">Short Description</th>
                        <th field="long_desc" editor="{type:'text'}" width="55%">Long Description</th>
                        <th field="lower_range" editor="{type:'numberspinner', options:{precision:1,min:1,max:5,increment:0.5,editable:false}}" width="5%">Min</th>
                        <th field="upper_range" editor="{type:'numberspinner', options:{precision:1,min:1,max:5,increment:0.5,editable:false}}" width="5%">Max</th>
                        <th field="actions" width="15%" align="center" formatter="formatActions"></th>
                    </tr>
                </thead>  
            </table> 
        </div>
        <div id="dd" title="New Recommendation" style="width:440px; height:310px">
            <table>
                <tr>
                    <td colspan="2"><font color="red">All fields are required</font><br/><br/></td>
                </tr>
                <tr>
                    <td>Theme:</td>
                    <td><select name="theme" id="theme"></select></td>
                </tr>
                <tr>
                    <td>Range:</td>
                    <td><input size="5" id="min" name="min" type="text" /> - <input size="5" id="max" name="max" type="text" /></td>
                </tr>
                <tr>
                    <td>Short<br/>Description:</td>
                    <td><textarea id="short" name="short" rows="1" cols="40"></textarea></td>
                </tr>
                <tr>
                    <td>Long<br/>Description:</td>
                    <td><textarea id="long" name="long" rows="4" cols="40"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <br/>
                        <input type="button" value="Create" onclick="createRecommendation()"/>
                        <input type="button" value="Cancel" onclick="closeDD()"/>
                    </td>
                </tr>
            </table> 
        </div>
        <div id="dd_update" title="Edit Recommendation" style="width:440px; height:310px">
            <table>
                <tr>
                    <td colspan="2"><font color="red">All fields are required</font><br/><br/></td>
                </tr>
                <tr>
                    <td>ID:</td>
                    <td><input size="5" id="recommend_id_update" name="recommend_id_update" type="text" readonly="readonly"/></td>
                </tr>
                <tr>
                    <td>Range:</td>
                    <td><input size="5" id="min_update" name="min_update" type="text" /> - <input size="5" id="max_update" name="max_update" type="text" /></td>
                </tr>
                <tr>
                    <td>Short<br/>Description:</td>
                    <td><textarea id="short_update" name="short_update" rows="1" cols="40"></textarea></td>
                </tr>
                <tr>
                    <td>Long<br/>Description:</td>
                    <td><textarea id="long_update" name="long_update" rows="4" cols="40"></textarea></td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <br/>
                        <input type="button" value="Update" onclick="updateRecommendation()"/>
                        <input type="button" value="Cancel" onclick="closeDDUpdate()"/>
                    </td>
                </tr>
            </table> 
        </div>
    </body>
</html>
